import { _decorator, Component, director, Node, UITransform, Vec3, Canvas, UI } from 'cc';
const { ccclass, property } = _decorator;

// 导入Game.ts
import { Game } from './Game';

@ccclass('Ground')
export class Ground extends Component {
    @property({ type: Node, tooltip: 'Frist Ground' }) // 绑定第一个地面节点 tooltip该属性在编辑器中的工具提示内容
    public ground1: Node = null!;
    @property({ type: Node, tooltip: 'Second Ground' })
    public ground2: Node = null!;
    @property({ type: Node, tooltip: 'Third Ground' })
    public ground3: Node = null!;

    // 地面的宽度
    public groundWidth1: number;
    public groundWidth2: number;
    public groundWidth3: number;

    // 临时开始位置
    public tempStartLocation1 = new Vec3();
    public tempStartLocation2 = new Vec3();
    public tempStartLocation3 = new Vec3();
    // 获取Game脚本类并实例化
    public gameCtrl = new Game();
    // 游戏速度
    public gameSpeed: number = 50;

    start() {}

    update(deltaTime: number) {
        this.updateGroundPosition(deltaTime);
    }

    startUp() {
        // 获取定义的地面宽度
        this.groundWidth1 = this.ground1.getComponent(UITransform).width;
        this.groundWidth2 = this.ground2.getComponent(UITransform).width;
        this.groundWidth3 = this.ground3.getComponent(UITransform).width;

        // 设置地面的临时初始位置
        this.tempStartLocation1.x = 0;
        this.tempStartLocation2.x = this.groundWidth1;
        this.tempStartLocation3.x = this.groundWidth1 + this.groundWidth2;

        // 更新地面的开始位置 - 使用setPostion()
        this.ground1.setPosition(this.tempStartLocation1);
        this.ground2.setPosition(this.tempStartLocation2);
        this.ground3.setPosition(this.tempStartLocation3);
    }

    updateGroundPosition(dt: number) {
        // 获取地面和背景的移动速度
        this.gameSpeed = this.gameCtrl.speed;

        // 将当前地块的实际位置赋值为临时位置
        this.tempStartLocation1 = this.ground1.position;
        this.tempStartLocation2 = this.ground2.position;
        this.tempStartLocation3 = this.ground3.position;

        // 地块向屏幕左方移动，实现屏幕向右的视觉效果
        // 根据帧数*调整移动速度和改变位置的x坐标
        this.tempStartLocation1.x -= this.gameSpeed * dt;
        this.tempStartLocation2.x -= this.gameSpeed * dt;
        this.tempStartLocation3.x -= this.gameSpeed * dt;

        // 获取Canvas的尺寸
        const scene = director.getScene(); // 获取当前逻辑场景
        const canvas = scene.getComponentInChildren(Canvas); // 递归查找所有子节点中第一个匹配指定类型的组件

        // 判断地块是否出界（离开屏幕边缘）， 如果出界，则把它移动到屏幕右侧的队列尾部，实现地面持续化视觉
        if (this.tempStartLocation1.x <= 0 - this.groundWidth1) {
            this.tempStartLocation1.x = canvas.getComponent(UITransform).width;
        }
        if (this.tempStartLocation2.x <= 0 - this.groundWidth2) {
            this.tempStartLocation2.x = canvas.getComponent(UITransform).width;
        }

        if (this.tempStartLocation3.x <= 0 - this.groundWidth3) {
            this.tempStartLocation3.x = canvas.getComponent(UITransform).width;
        }

        //   设置新位置给地块节点
        this.ground1.setPosition(this.tempStartLocation1);
        this.ground2.setPosition(this.tempStartLocation2);
        this.ground3.setPosition(this.tempStartLocation3);
    }
}
